package attori.proattivi;

import gui.houseeditor.Stanza;

import java.util.List;
import java.util.Random;

import sde.actor.Actor;
import sde.actor.Message;
import sde.actor.Timer;

public class ModificatoreTemperatura extends Actor {
	public static final byte OPERATIVE = 1, IDLE = 0;
	private List<Stanza> s;

	private static class Timeout extends Message {
	}

	public static class Accendi extends Message {
	}

	public static class Spegni extends Message {
	}

	private Timer timer = new Timer();
	private Timeout timeout = new Timeout();
	private long periodo;

	public ModificatoreTemperatura(List<Stanza> stanza, long periodo) {
		super();
		this.s = stanza;
		this.periodo = periodo;
		become(OPERATIVE);
		timer.set(timeout, this, periodo);
	}

	@Override
	protected void handler(Message m) {
		switch (currentStatus()) {
		case OPERATIVE:
			if (m == timeout) {
				if (Math.random() > 0.5) {
					for (Stanza st : s) {
						if (st.getTemperatura() <= 40) {
							st.setTemperatura(st.getTemperatura()
									+ new Random().nextInt(3)+10);
						}
					}
				} else {
					for (Stanza st : s) {
						if (st.getTemperatura() > -5) {
							st.setTemperatura(st.getTemperatura()
									- new Random().nextInt(3)-5);
						}
					}

				}
				timer.set(m, this, periodo);
			} else if (m instanceof Spegni) {
				become(IDLE);
			}
			break;
		case IDLE:
			if (m instanceof Accendi) {
				become(OPERATIVE);
				send(timeout);
			}
			break;
		}

	}
}
